package com.pioneer.swing.model;

import com.pioneer.dao.ManifestDAO;
import com.pioneer.dao.DataAccessException;
import com.pioneer.beans.ContainerContent;
import com.pioneer.beans.Key;
import com.pioneer.beans.ManifestKey;
import com.pioneer.beans.Container;
import com.pioneer.swing.ui.UIConstants;
import javax.swing.table.DefaultTableModel;
import java.util.Vector;
import java.util.List;
import java.util.Iterator;
import java.text.NumberFormat;
import java.text.DecimalFormat;

/**
 * Title:        Pioneer Metals Application
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:      Pioneer Metals
 * @author Gary Chen
 * @version 1.0
 */

public class AllContainersTable extends DefaultTableModel {

	private ManifestDAO dao;
	private String jobNum;
	private NumberFormat numFormat;

	public static final int CONTAINER_ID=0;
	public static final int DESCRIPTION=1;
	public static final int PACKING=2;
	public static final int FOB_C_NUM=3;	
	public static final int WEIGHT=4;
	public static final int COST=5;
	public static final int FOB_AMOUNT=6;

    public AllContainersTable(ManifestDAO dao,Key key) {
		columnIdentifiers=new Vector();
		columnIdentifiers.add(TableConstants.CONTAINER_ID);
		columnIdentifiers.add(TableConstants.DESCRIPTION);
		columnIdentifiers.add(TableConstants.PACKING);
		//columnIdentifiers.add(TableConstants.FOB);
		columnIdentifiers.add(TableConstants.FOB_C_NUM);
		columnIdentifiers.add(TableConstants.WEIGHT);
		columnIdentifiers.add(TableConstants.COST);
		columnIdentifiers.add(TableConstants.FOB_AMOUNT);
		this.dao=dao;
		if (key!=null)
		{
		    String jobNum=((ManifestKey)key).getJobNum();
			this.jobNum=jobNum;
		    if (jobNum!=null) refresh();
		}
    }

	public void updateJobNum(String jobNum) {
		this.jobNum=jobNum;
	}

	/*public Object getValueAt(int row, int column) {
		// if the column to add
		if (column==6) {
			numFormat=new DecimalFormat("###,###.##");
			try {
				 return numFormat.parse(super.getValueAt(row,column).toString());
			}
			catch (java.text.ParseException ex) {
				ex.printStackTrace(System.err);
			}

		}
		return super.getValueAt(row,column);
	}*/

	private String formatNum(Object obj,String patternFormat) {
		double toFormat=((Float)obj).doubleValue();
		numFormat=new DecimalFormat(patternFormat);
		StringBuffer buf=numFormat.format(toFormat,new StringBuffer(),new java.text.FieldPosition(0));
		return buf.toString();
	}

	public void refresh()
	{
		try
		{
			//System.out.println("AllContainersTable refreshing with jobNum "+jobNum);
		    List containers=dao.getContainersWithContent(jobNum);
			// all rows
			dataVector=new Vector();
			Iterator rows=containers.iterator();
			while (rows.hasNext())
			{
				Container container=(Container)rows.next();
				List contents=container.getContents();
				ContainerContent content=null;
				for (int count=0;count<contents.size();count++)
				{
				    Vector row=new Vector();
				    row.add(container.getId());
					content=(ContainerContent)contents.get(count);
					row.add(content.getDescription());
					row.add(content.getRemarks());
					//row.add(content.getFob());
					row.add(formatNum(new Float(content.getFobCNumber()),UIConstants.PATTERN_DEC_FORMAT_4));
					row.add(formatNum(new Float(content.getWeight()),UIConstants.PATTERN_DEC_FORMAT_2));
					row.add(formatNum(new Float(content.getCost()),UIConstants.PATTERN_DEC_FORMAT_3));
					row.add(formatNum(new Float(content.getFobAmount()),UIConstants.PATTERN_DEC_FORMAT_2));
					dataVector.add(row);
				}
			}
		}
		catch (DataAccessException ex)
		{
			ex.printStackTrace(System.err);
		}
	}

	public boolean isCellEditable(int rowIndex,
                              int columnIndex) {
		return false;
	}


    public static void main(String[] args) {
        //AllContainersTable allManifestsTable1 = new AllContainersTable();
    }
}